﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaobaoERP.BusinessTaobao.ParObject;
using TaobaoERP.Core.Common;

namespace TaobaoERP.BusinessTaobao.Common
{
    public class APIUtils
    {
        public delegate TData Actions<TData>(string result);
        public static TData Result<TData>(ParBase par, Actions<TData> reslutAction)
        {

            string status = string.Empty;
            string result = Signature.Post(Constants.SIP_API_URL, par.ToString(), out status);
            if (string.IsNullOrEmpty(status))
            {
                return default(TData);
            }
            else if ("9999".Equals(status))
            {
                return reslutAction(result);
                //return null;
            }
            else if ("1004".Equals(status))
            {
                System.Web.HttpContext.Current.Response.Redirect(result + "&sip_redirecturl=" + System.Web.HttpContext.Current.Request.Url);
                return default(TData); ;
            }
            else
            {

                //Utils.Debug("itemcats_get:  " + parent_cid + " : " + Signature.GetMessage(status));
                return default(TData); ;
            }
        }
        private bool IsSuccess(string status)
        {
            if (status == "9999")
            {
                return true;
            }
            else if ("1004".Equals(status))
            {
                //System.Web.HttpContext.Current.Response.Redirect(result + Constants.SIP_Redirecturl);
                return false ;
            }
            else
            {
                return false;
            }
        }
        private Dictionary<string, string> GetErrorState()
        {
            Dictionary<string, string> error = new Dictionary<string, string>();
            error.Add("1001", "签名无效");
            error.Add("1002", "请求已过期");
            error.Add("1004", " 需要绑定用户");
            //error.Add("1001", "签名无效");
            //error.Add("1001", "签名无效");
            //error.Add("1001", "签名无效");
            //error.Add("1001", "签名无效");
            //error.Add("1001", "签名无效");
            //error.Add("1001", "签名无效");
            //error.Add("1001", "签名无效");
            //error.Add("1001", "签名无效");
            //error.Add("1001", "签名无效");
            //error.Add("1001", "签名无效");
            //error.Add("1001", "签名无效");
            //error.Add("1001", "签名无效");
            error.Add("0000", "未知异常");
            error.Add("9999", "签名无效");
            return error;
        }
    }
}
